{% extends "admin/layout_admin.html" %}
{% block content %}
{% if obj %}
<!-- rich editer -->
<script type="text/javascript" src="/static/jquery-plugin/jquery-1.6.4.js"></script>
<script type="text/javascript" src="/static/markitup/jquery.markitup.js"></script>
<script type="text/javascript" src="/static/markitup/sets/html/set.js"></script>
<link rel="stylesheet" type="text/css" href="/static/markitup/skins/simple/style.css" />
<link rel="stylesheet" type="text/css" href="/static/markitup/sets/html/style.css" />
<link rel="stylesheet" type="text/css" href="/static/uploadify/uploadify.css" />
<script type="text/javascript">
<!--
$(document).ready(function()	{
	// Add markItUp! to your textarea in one line
	$('#html').markItUp(myHtmlSettings);
});
-->
</script>

<!-- rich editer end -->

<!-- facebox -->
<link href="/static/facefiles/facebox.css" media="screen" rel="stylesheet" type="text/css" />
<script src="/static/facefiles/facebox.js" type="text/javascript"></script>

<script type="text/javascript">
    jQuery(document).ready(function($) {
      $('a[rel*=facebox]').facebox()
	  //$('a[rel*=facebox]').click(); 
    })
</script>

<!-- /facebox -->

<?py use_default_theme = ('default' == THEME) ?>

<span id="h"></span>
<form action="" method="post" id="commentform">
<h2>编辑{{obj._id}}号文章 (<a href="/detailpost/{{obj._id}}" target="_blank">查看该文章</a> | <a href="#deldiv" rel="facebox[.thickstyle]">删除该文章</a>)</h2>
<p id="text" style="color:red;"></p>

<p>*分类: <input type="text" name="category" id="cat" style="width:150px;" value="{{obj._category}}" tabindex="1" /> 17s <a href="#catdiv" rel="facebox[.thickstyle]">从已有分类中选择</a></p>

<p>*标题: <input type="text" name="title" id="tit" style="width:430px;" value="{{obj._title}}" tabindex="2" /> 100s</p>
<textarea id="html" name="content" style="width:500px;height:300px;" tabindex="3">{{obj._content}}</textarea>

上传图片或附件 <input id="filetoupload" type="file" size="45" name="filetoupload" class="input">
<p>标签: <input type="text" name='tags' id="tag" style="width:360px;" value="{{obj._tags}}" tabindex="4" /> 100s 用逗号分隔','<br/>
<a href="#tagdiv" rel="facebox[.thickstyle]">从最近标签中选择</a>
</p>
<p>

<?py ck = ['','checked'] ?>

<label>关闭评论 <input type="checkbox" id="clo" value="True" tabindex="5" #{ck[obj.closecomment]} /></label>

 | 设置阅读密码: <input type="text" name='password' id="password" style="width:150px;" value="{{obj._password}}" tabindex="6" /> 8s
<input type='hidden' value='' id="password" />

</p>
<p><button id="submit1" class="bigbtn"> 提交修改 </button></p>

<script>
$('#submit').click(function(){
    
    var cat = $('#cat').val().replace(/(^\s*)|(\s*$)/g,"");
	var tit = $('#tit').val().replace(/(^\s*)|(\s*$)/g,"");
	var con = $('#html').val().replace(/(^\s*)|(\s*$)/g,"");
	var tag = $('#tag').val().replace(/(^\s*)|(\s*$)/g,"");
	var password = $('#password').val().replace(/(^\s*)|(\s*$)/g,"");
	
	var t = $('#text');
	
	if(cat.length< 1 || cat.length > 17){
	    $('#cat').focus();
		t.html("分类 必填 且最多可填17个字符");
	    return;
	}
	
	if(tit.length< 1 || tit.length > 100){
	    $('#tit').focus();
		t.html("标题 必填 且最多可填100个字符");
	    return;
	}
	
	if(con.length< 1){
		document.getElementsByTagName('textarea')[0].focus();
		t.html("内容 必填");
	    return;
	}
		
	if(tag.length > 100){
	    $('#tag').focus();
		t.html("标签最多可填100个字符");
	    return;
	}
	
	if(password.length > 8){
	    $('#password').focus();
		t.html("密码最多可设8个字符");
	    return;
	}
		
	$('#submit1').attr('disabled', 'disabled');
	t.html("Sending now.....");
	$('#commentform').submit();    
});

function setcat(cat){
    var catField = document.getElementById('cat');
	catField.value = cat;
}

function settag(tag){
    var tagField = document.getElementById('tag');
	tagField.value += tag + ",";
}

</script>
<input type='hidden' value='editpost' name='act' />
<input type='hidden' value='{{ obj._id }}' name='id' />
</form>
{% endif %}

<div id="catdiv" style="display:none;line-height:1.7em;color:#999">
<h4>请选择以下已有分类</h4>
{% for cat in cats %}
<a href="#" onclick="setcat('{{cat.name}}');return false;">{{cat.name}}</a>, 
{% endfor %}
</div>

<div id="tagdiv" style="display:none;line-height:1.7em;color:#999">
<h4>请选择以下已有标签</h4>
{% for tag in tags %}
<a href="#" onclick="settag('{{tag.name}}');return false;">{{tag.name}}</a>, 
{% endfor %}
</div>

<div id="deldiv" style="display:none">
<h4>你一定要删除这篇文章吗？</h4>
<p>不推荐删除，建议 编辑文章 或 通过设置查看密码以屏蔽。</p>
<?py if STORAGE_DOMAIN_NAME in obj.content: ?>
<p style="color:red">本文内容可能含有附件，若要删除附件请到SAE 后台删除。</p>
<?py #endif ?>
<p>我想好了，<a href="/admin/del_post/{{obj._id}}">一定要删除它，不后悔</a>！！</p>
</div>

<!-- ajaxfileupload -->
<script src="/static/uploadify/swfobject.js" type="text/javascript" charset="utf-8"></script>
<script src="/static/uploadify/jquery.uploadify.v2.1.4.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
$('#filetoupload').uploadify({
    'uploader'  : '/static/uploadify/uploadify.swf',
    'script'    : '/uploadFile',
    'cancelImg' : '/static/uploadify/cancel.png',
    'folder'    : '/uploads',
	'fileExt'   : '*.txt;*.pdf;*.png;*.jpg;*.jpeg;*.gif;*.doc;*.rar',
	'fileDesc'  : 'Image,Txt,doc,pdf,rar Files',
	'sizeLimit' : 2048000,//2M
    'auto'      : true,
	'queueID'   : 'fileQueue',
	'multi'     : false, //是否允许同时上传多文件，默认false
	'simUploadLimit' : 1, //一次同步上传的文件数目  
	'queueSizeLimit' : 3, //限制在一次队列中的次数（可选定几个文件）。默认值= 999，而一次可传几个文件有 simUploadLimit属性决定。
	'buttonText'     : 'BROWSE',
	onComplete: function (event, queueID, fileObj, response, data) {
		var el = document.getElementById('html');
        var filetype = fileObj.name.toLowerCase().split(".");
        var tp ="png,jpg,jpeg,gif";
        var rs=tp.indexOf(filetype[filetype.length-1]);
        if(rs>=0){
             el.value += '<p>图片：' + fileObj.name + '<img src="' + response + '" /> </p>';
        }else{
             el.value += '<p>附件：' + fileObj.name + ' <a href="' + response + '" target="_blank">点击下载</a> </p>';
        }
        el.focus();
	}  
	});
</script>
<!-- /ajaxfileupload -->

<?py else: ?>

<form action="" method="post">
<p>
输入要编辑的文章 id <input type="text" value="" name="id" size="10" />
<input type='hidden' value='findid' name='act' />
<input type="submit" value=" 提 交 " name="submit" />
</p>
<p>如何知道文章id？示例文章网址如下，红色的字就是该文章的id：</p>
<p>地址栏里的网址 http://chartnet.sinaapp.com/detailpost/<span class="red">12</span></p>
</form>

<?py #endif ?>
{% endblock %}